{% extends 'form_div_layout.html.twig' %}
{% from 'MopaBootstrapBundle::flash.html.twig' import flash %}

{# legends & labels #}

{% block form_legend %}
{% spaceless %}
          <legend>{{ label | trans }}</legend>
{% endspaceless %}
{% endblock form_legend %}

{% block generic_label %}
{% spaceless %}
    {% if required %}
        {% set attr = attr|merge({'class': attr.class|default('') ~ ' required'}) %}
    {% endif %}
    {% set attr = attr|merge({'class': attr.class|default('') ~ ' control-label'}) %}
    <label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
    {{- label|trans -}}
    {% if required %}*{% endif %}
    {% if widget_add_btn %}
        {{ block('field_widget_add_btn') }}
    {% endif %}
    {% if help_label %}
        <p class="help-block">{{ help_label|trans }}</p>
    {% endif %}
    </label>
{% endspaceless %}
{% endblock %}

{# renders a field label without span class, if e.g field has it#}

{% block field_label_attr_aware_label %}
{% spaceless %}
    {% if label_render %}
    {% set attr = label_attr|merge({'for': id}) %}
    {{ block('field_label') }}
    {% endif %}
{% endspaceless %}
{% endblock field_label_attr_aware_label %}

{# Help #}

{% block field_help %}
{% spaceless %}
    {% if help_inline %}<p class="help-inline">{{ help_inline|trans|raw }}</p>{%endif %}
    {% if help_block %}<p class="help-block">{{ help_block|trans|raw }}</p>{%endif %}
{% endspaceless %}
{% endblock field_help %}

{% block field_widget_add_btn %}
{% spaceless %}
    {% if widget_add_btn %}
    <input type="button" class="btn" data-collection-add-btn="{{ form.get('id')}}_control_group" value="{{ widget_add_btn }}">
    {% endif %}
{% endspaceless %}
{% endblock field_widget_add_btn %}

{% block field_widget_remove_btn %}
{% spaceless %}
    {% if widget_remove_btn %}
    <input type="button" class="btn" data-collection-remove-btn="{{ form.parent.get('id')}}_control_group" value="{{ widget_remove_btn }}">
    {% endif %}
{% endspaceless %}
{% endblock field_widget_remove_btn %}

{# Errors #}

{% block form_errors %}
{% spaceless %}
{% if error_delay %}
    {% for child in form %}
        {% if loop.index == 1 %}
            {% if child.set('errors', errors) %}{% endif %}
        {% endif %}
    {% endfor %}
{% else %}
{# prevent deep nesting wrong context copy error #}
{% from 'MopaBootstrapBundle::flash.html.twig' import flash %}
    {% for error in errors %}
		{{ flash('error', error.messageTemplate|trans(error.messageParameters, 'validators')) }}
	{% endfor %}
{% endif %}
{% endspaceless %}
{% endblock form_errors %}

{% block field_errors %}
{% spaceless %}
    {% if errors|length > 0 %}
    <span class="help-{{ block('error_type') }}">
            {% for error in errors %}
                {{ error.messageTemplate|trans(error.messageParameters, 'validators') }} <br>
            {% endfor %}
    </span>
    {% endif %}
{% endspaceless %}
{% endblock field_errors %}

{# used to determine which type of error #}

{% block error_type %}
{% spaceless %}
{% if field_error_type %}
{{ field_error_type }}
{% elseif form.hasParent() != 0 %}
    {% set form = form.parent %}
    {{ block('error_type') }}
{% elseif form.hasParent() == 0 %}
    {{ form.get('error_type') }}
{% endif %}
{% endspaceless %}
{% endblock error_type %}

{# Basic form and field blocks #}

{% block form_widget %}
{% spaceless %}
    {% if 'form' in form.get('types') and form.hasParent() == 0 %}
    	{% if render_fieldset %}<fieldset>{% endif %}
    	{% if show_legend %}{{ block('form_legend') }}{% endif %}
    {% endif %}
        {{ block('field_rows_visible') }}
        {{ form_rest(form) }}
    {% if 'form' in form.get('types') and form.hasParent() == 0 %}
    	{% if render_fieldset %}</fieldset>{% endif %}
    {% endif %}
{% endspaceless %}
{% endblock form_widget %}

{# only display non hidden fields #}

{% block field_rows_visible %}
{% spaceless %}
    {% if form_errors(form) %}
        <div class="symfony-form-errors">
            {{ form_errors(form) }}
        </div>
    {% endif %}
    {% for child in form.children %}
        {% if 'hidden' not in child.get('types') %}
            {{ form_row(child) }}
        {% endif %}
    {% endfor %}
{% endspaceless %}
{% endblock field_rows_visible %}

{% block field_row %}
{% spaceless %}
    {% set _isCollectionItem = (form.hasParent()|default(false) and form.getParent().get('prototype') ) %}
	{% set _isSubform = ('form' in form.get('types')) %}
	{% set attr = attr|merge({'class': attr.class|default('') ~ (errors|length > 0 ? ' error' : '') }) %}
    {% if not _isSubform %}
        {% if widget_control_group and not _isCollectionItem %}
            {% set oid = id %}
            {% set id = oid ~ "_control_group" %}
    <div class="control-group{% if errors|length > 0 %} error{% endif %}" {{ block('widget_attributes') }}>
            {% set id = oid %}
        {% endif %}
    {% endif %}
    {% if _isSubform and form.hasParent() != 0 %}
        {% if show_child_legend|default(false) %}
            {{ block('form_legend') }}
        {% endif %}
	{% elseif not _isCollectionItem  %}
    	{{ block('field_label_attr_aware_label') }}
    {% endif %}
    {% if not _isSubform %}
    	{% if widget_controls and not _isCollectionItem %}
        <div class="controls">
        {% endif %}
    {% endif %}
    {% if _isCollectionItem %}
		<div class="collection-item">
    {% endif %}
        {{ widget_prefix|raw }} {{ form_widget(form, {'label': label, 'label_render': label_render, 'widget_remove_btn': widget_remove_btn, 'attr': attr, 'widget_addon': widget_addon, 'widget_type': widget_type, 'help_inline': help_inline, 'help_block': help_block, 'widget_control-group': widget_control_group, 'prototype': form.get('prototype')}) }} {{ widget_suffix|raw }}
    {% if widget_remove_btn is defined %}
        {{ block('field_widget_remove_btn') }}
    {% endif %}
    {% if not _isSubform %}
           {{ block('field_message') }}
    {% endif %}
    {% if _isCollectionItem %}
		</div>
    {% endif %}
    {% if not _isSubform %}
        {% if widget_controls and not _isCollectionItem %}
        </div>
        {% endif %}
    {% endif %}
    {% if not _isSubform %}
	    {% if widget_control_group and not _isCollectionItem %}
    </div>
        {% endif %}
    {% endif %}
{% endspaceless %}
{% endblock field_row %}

{% block field_message %}
{% spaceless %}
    {{ form_errors(form) }}
    {{ block('field_help') }}
{% endspaceless %}
{% endblock field_message %}

{# basic form widgets #}

{% block field_widget %}
{% spaceless %}
    {% set type = type|default('text') %}
    {% if widget_addon.text|default(false) or widget_addon.icon|default(false) %}
    <div class="input-{{ widget_addon.append ? 'append':'prepend' }}">
    	{% if not widget_addon.append %}
    	{{ block('widget_addon') }}
    	{% endif %}
    {% endif %}
    {# add class to style display block #}
    {% if not widget_remove_btn|default(false) %}
        {% set attr = attr|merge({'class': attr.class|default('') ~ ' not-removable'}) %}
    {% endif %}
    	<input type="{{ type }}" {{ block('widget_attributes') }} value="{{ value }}" />
    {% if widget_addon.text|default(false) or widget_addon.icon|default(false) %}
    	{% if widget_addon.append %}
    	{{ block('widget_addon') }}
    	{% endif %}
    {% endif %}
    {% if widget_addon.text|default(false) or widget_addon.icon|default(false) %}
    </div>
    {% endif %}
{% endspaceless %}
{% endblock field_widget %}

{% block widget_addon %}
{% spaceless %}
{# prevent deep nesting wrong context copy error #}
{% from 'MopaBootstrapBundle::icons.html.twig' import icon %}
<span class="add-on">{{ (widget_addon.text ? widget_addon.text : icon(widget_addon.icon))|raw  }}</span>
{% endspaceless %}
{% endblock widget_addon %}

{% block collection_row %}
{% spaceless %}
	{% set _isSubform = ('form' in form.get('types')) %}
	{% set attr = attr|merge({'class': attr.class|default('') ~ (errors|length > 0 ? ' error' : '') }) %}
    {% if widget_control_group %}
		{% if prototype is defined %}
			{% set attr = attr|merge({'data-prototype': form_row(prototype), 'data-collection': 'true' }) %}
		{% endif %}
        {% set oid = id %}
        {% set id = oid ~ "_control_group" %}
    <div class="collection control-group{% if errors|length > 0 %} error{% endif %}" {{ block('widget_attributes') }}>
        {% set id = oid %}
    {% endif %}
	{{ block('field_label_attr_aware_label') }}
   	{% if widget_controls %}
        <div class="collection controls">
    {% endif %}    
    {{ form_widget(form) }}
   	{% if widget_controls %}
        </div>
    {% endif %}    
    {% if widget_control_group %}
    </div>
    {% endif %}
{% endspaceless %}
{% endblock collection_row %}

{% block collection_widget %}
{% spaceless %}
    {% for child in form.children %}
        {% if 'hidden' not in child.get('types') %}
			{#% debug child.get('attr') %#}
            {#{ form_row(child) }#}
        {% endif %}
    {% endfor %}
    {% if errors|length > 0 %}
        {% set attr = attr|merge({'class': attr.class|default('') ~ ' error'}) %}
    {% endif %}
    {% set coll_item = "BlUB" %}
    {{ block('form_widget', coll_item) }}
{% endspaceless %}
{% endblock collection_widget %}	

{# more form widgets #}

{% block textarea_widget %}
{% spaceless %}
	{% set attr = attr %}
    <textarea {{ block('widget_attributes') }}>{{ value|escape('js') }}</textarea>
{% endspaceless %}
{% endblock textarea_widget %}

{% block choice_widget %}
{% spaceless %}
    {% if expanded %}
    <div class="control-group {% if errors|length > 0 %}error{% endif %}">
    	{% if label_render %}
    	{{ form_label(form, label) }}
    	{% endif %}
        <div class="controls">
            {% for child in form %}
                <label class="{{ widget_type ~ (multiple ? ' checkbox' : ' radio') }}">
                    {{ form_widget(child, {'attr': {'class': attr.widget_class|default('')}}) }}
            		{{ child.get('label')|trans }}
            	</label>
            {% endfor %}
            {{ block('field_message') }}
        </div>
    </div>
    {% else %}
            <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
                {% if empty_value is not none %}
                <option value="">{{ empty_value|trans }}</option>
                {% endif %}
                {% if preferred_choices|length > 0 %}
                    {% set options = preferred_choices %}
                    {{ block('widget_choice_options') }}
                    {% if choices|length > 0 %}
                <option disabled="disabled">{{ separator }}</option>
                    {% endif %}
                {% endif %}
                {% set options = choices %}
                {{ block('widget_choice_options') }}
            </select>
    {% endif %}
{% endspaceless %}
{% endblock choice_widget %}

{# Date and Time widgets #}

{% block date_widget %}
{% spaceless %}
    {% if widget == 'single_text' %}
        {{ block('field_widget') }}
    {% else %}
    <div class="control-group {% if errors|length > 0 %}error{% endif %}">
    	{{ form_label(form, label) }}
        <div class="controls">
            {% set attr = attr|merge({'class': attr.class|default('')}) %}
	        <div {{ block('widget_container_attributes')  }}>
	            {{ date_pattern|replace({
                    '{{ year }}':  form_widget(form.year, {'attr': {'class': attr.widget_class|default('') ~ ' input-mini'}}),
                    '{{ month }}': form_widget(form.month, {'attr': {'class': attr.widget_class|default('') ~ ' input-small'}}),
                    '{{ day }}':   form_widget(form.day, {'attr': {'class': attr.widget_class|default('') ~ ' input-mini'}}),
	            })|raw }}
	        </div>
	        {{ block('help') }}
        </div>
    </div>
    {% endif %}
{% endspaceless %}
{% endblock date_widget %}

{% block time_widget %}
{% spaceless %}
    {% if widget == 'single_text' %}
        {{ block('field_widget') }}
    {% else %}
        <div class="control-group {% if errors|length > 0 %}error{% endif %}">
            {{ form_label(form, label) }}
            <div class="controls">
                {% set attr = attr|merge({'class': attr.class|default('')}) %}
                <div {{ block('widget_container_attributes')  }}>
                    {{ form_widget(form.hour, { 'attr': { 'class': 'input-mini' } }) }}:{{ form_widget(form.minute, { 'attr': { 'class': 'input-mini' } }) }}{% if with_seconds %}:{{ form_widget(form.second, { 'attr': { 'class': 'input-mini' } }) }}{% endif %}
                </div>
                {{ block('help') }}
            </div>
        </div>
    {% endif %}
{% endspaceless %}
{% endblock time_widget %}

{% block datetime_widget %}
{% spaceless %}
    {% if widget == 'single_text' %}
        {{ block('field_widget') }}
    {% else %}
    <div class="control-group {% if errors|length > 0 %}error{% endif %}">
    	{{ form_label(form, label) }}
        <div class="controls">
            {% set attr = attr|merge({'class': attr.class|default('')}) %}
	        <div {{ block('widget_container_attributes') }}>
	            {{ form_errors(form.date) }}
	            {{ form_errors(form.time) }}
	            {{ form_widget(form.date, {'attr': {'class': attr.widget_class|default('')}}) }}
	            {{ form_widget(form.time, {'attr': {'class': attr.widget_class|default('')}}) }}
	        </div>
	        {{ block('field_help') }}
        </div>
    </div>
    {% endif %}
{% endspaceless %}
{% endblock datetime_widget %}

{# widget attributes #}

{% block widget_attributes %}
{% spaceless %}
    id="{{ id }}" name="{{ full_name }}"{% if read_only %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}
    {% for attrname,attrvalue in attr %}{{attrname}}="{{attrvalue|trans}}" {% endfor %}
{% endspaceless %}
{% endblock widget_attributes %}
